﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace CalculationDemo
{
    public partial class MainPage : UserControl
    {
        
        private DateTime ct = new DateTime(2013,1,1);
        private NewCalculation calculation = new NewCalculation();
        
        List<string> layerList;
        public List<string> LayerList
        {
            get { return layerList; }
            set { layerList = value; }
        }

        List<UnitLayer> ulList;
        public List<UnitLayer> UlList
        {
            get { return ulList; }
            set { ulList = value; }
        }

        ObservableCollection<UnitLayerCapacity> pList = new ObservableCollection<UnitLayerCapacity>();
        public ObservableCollection<UnitLayerCapacity> PList
        {
            get { return pList; }
            set { pList = value; }
        }

        ObservableCollection<UnitLayerCapacity> iList = new ObservableCollection<UnitLayerCapacity>();
        public ObservableCollection<UnitLayerCapacity> IList
        {
            get { return iList; }
            set { iList = value; }
        }

        ObservableCollection<FIFPAppointment> planList;
        public ObservableCollection<FIFPAppointment> PlanList
        {
            get { return planList; }
            set { planList = value; }
        }

        public MainPage()
        {
            InitializeComponent();

            CalculateDate.Text = String.Format("{0:yyyy-M-d}", ct); 
            calculation.LoadUnitsS4WithStrings();
            LayerList = calculation.LayerUnitsDictionary.Keys.ToList<string>();
            PlanList = new ObservableCollection<FIFPAppointment>(calculation.ImportPlans);     

        }

        private void layerGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
        }

        private void pStringGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
        }

        private void iStringGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
        }

        private void planGrid_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this;
        }   

        private void layerGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int index = layerGrid.SelectedIndex;
            DateTime currentDay = Convert.ToDateTime(CalculateDate.Text);
            double pSum = 0;
            double iSum = 0;


            if (index >= 0)
            {
                string layerName = (string)layerGrid.SelectedItem;
                ulList = calculation.LayerUnitsDictionary[layerName];

                pList.Clear();
                iList.Clear();
                pSum = 0;
                iSum = 0;

                foreach (UnitLayer ul in ulList)
                {
                    UnitLayerCapacity uLC = new UnitLayerCapacity();
                    Unit u = calculation.getUnitByUID(ul.unitID);

                    uLC.unitName = u.UnitName;
                    uLC.percentage = ul.percentage;
                    uLC.capacity = calculation.getCurrentDayStringCapacity(u, currentDay);
                    uLC.realCapacity = u.RealCapacity;
                    uLC.currentDayCapacity = uLC.percentage * uLC.realCapacity;
                   
                    if (u.FluidType == FIFPConstants.OIL)
                    {
                        pList.Add(uLC);
                        pSum += uLC.currentDayCapacity;
                    }
                    else
                    {
                        iList.Add(uLC);
                        iSum += uLC.currentDayCapacity;
                    }
                }

                pSumTextBlock.Text = pSum.ToString();
                iSumTextBlock.Text = iSum.ToString();
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //pre calculate
            foreach (Unit tu in calculation.AllUnitList)
            {
                tu.EffectiveCapacity = 0;
                tu.RealCapacity = 0;
                tu.IsBottleNeck = false;

                if (tu.UnitType == FIFPConstants.UNITTYPE_TOWER || tu.UnitType == FIFPConstants.UNITTYPE_WELL || tu.UnitType == FIFPConstants.UNITTYPE_STRING)
                    tu.Capacity = 0;
            }

            //calculate inj first, then prod
            calculation.CalculateWaterStringCurrentDayRealCapacity(Convert.ToDateTime(CalculateDate.Text));
            calculation.CalculateGasWellCurrentDayRealCapacity(Convert.ToDateTime(CalculateDate.Text));
            calculation.CalculateOilNetworkCurrentDayEffectiveCapacity(Convert.ToDateTime(CalculateDate.Text));

            MessageBox.Show("Calculation finished");
        }

        private void WaterNetworkButton_Click(object sender, RoutedEventArgs e)
        {
            WaterDistribution wdDialog = new WaterDistribution();
            wdDialog.UnitListForTest = calculation.AllUnitList.Where(p => p.FluidType == FIFPConstants.WATER).ToList();
            wdDialog.UpdateChart();
            wdDialog.Show();
        }

        private void OilNetworkButton_Click(object sender, RoutedEventArgs e)
        {
            OilNetwrokCalculate ocDialog = new OilNetwrokCalculate();
            ocDialog.UnitListForTest = calculation.AllUnitList.Where(p => p.FluidType == FIFPConstants.OIL).ToList();
            ocDialog.UpdateChart();
            ocDialog.Show();
        }

            
    }
}
